function showManageBookPage(arrBooks){
	var key = "product";
		addActive(key);
		var rootNode = $id("loadBook");
		rootNode.innerHTML = "";
		if (arrBooks.length > 0) {
			//create new table
			var nodeTable = createTable();
			nodeTable.setAttribute("id", "tableProduct");
			var nodeThId = createNode("th");
			var IdTh = createTextNode("Mã sách");
			nodeThId.appendChild(IdTh);
			var nodeThBName = createNode("th");
			var bNameTh = createTextNode("Tên sách");
			nodeThBName.appendChild(bNameTh);
			var nodeThCate = createNode("th");
			var cateTh = createTextNode("Thể loại");
			nodeThCate.appendChild(cateTh);
			var nodeThAu = createNode("th");
			var auTh = createTextNode("Tác giả");
			nodeThAu.appendChild(auTh);
			var nodeThPrice = createNode("th");
			var priceTh = createTextNode("Giá");
			nodeThPrice.appendChild(priceTh);
			var nodeThDes = createNode("th");
			var desTh = createTextNode("Mô tả");
			nodeThDes.appendChild(desTh);
			var nodeThQuan = createNode("th");
			var quanTh = createTextNode("Số lượng");
			nodeThQuan.appendChild(quanTh);
			var nodeThImg = createNode("th");
			var imgTh = createTextNode("Hình ảnh");
			nodeThImg.appendChild(imgTh);
			var nodeThCount = createNode("th");
			var countTh = createTextNode("Lượt xem");
			nodeThCount.appendChild(countTh);
			var nodeThUpdate = createNode("th");
			var nodeThDelete = createNode("th");

			nodeTable.appendChild(nodeThId);
			nodeTable.appendChild(nodeThBName);
			nodeTable.appendChild(nodeThCate);
			nodeTable.appendChild(nodeThAu);
			nodeTable.appendChild(nodeThPrice);
			nodeTable.appendChild(nodeThDes);
			nodeTable.appendChild(nodeThQuan);
			nodeTable.appendChild(nodeThImg);
			nodeTable.appendChild(nodeThCount);
			nodeTable.appendChild(nodeThUpdate);
			nodeTable.appendChild(nodeThDelete);

			var nodeTr = null;
			var book = null;
			var nodeId = null;
			var nodeBName = null;
			var nodeCate = null;
			var nodeAu = null;
			var nodePrice = null;
			var nodeDes = null;
			var nodeQuan = null;
			var nodeCount = null;
			var nodeTdImg = null;
			var nodeImg = null;
			var nodeUpdate = null;
			var nodeTdUpdate = null;
			var nodeDivUpdate = null;
			var nodeDelete = null;
			var nodeTdDelete = null;
			var nodeDivDelete = null;
			var length = arrBooks.length;

			for (var i = 0; i < length; i++) {
				book = arrBooks[i];
				nodeTr = createNode("tr");
				nodeTr.setAttribute("id", "book" + book.bookId);

				//create node bookid
				nodeId = createNode("td");
				var bookid = createTextNode(book.bookId);
				nodeId.appendChild(bookid);

				//create node book name
				nodeBName = createNode("td");
				var bookName = createTextNode(book.bookName);
				nodeBName.appendChild(bookName);

				//create node category
				nodeCate = createNode("td");
				var category = createTextNode(book.categoryId);
				nodeCate.appendChild(category);

				//create node author
				nodeAu = createNode("td");
				var author = createTextNode(book.author);
				nodeAu.appendChild(author);

				//create node price
				nodePrice = createNode("td");
				var price = createTextNode(book.price);
				nodePrice.appendChild(price);

				//create node description
				nodeDes = createNode("td");
				nodeDivDes = createDiv();
				var des = createTextNode(book.des);
				nodeDivDes.addClass("heightTd");
				nodeDivDes.appendChild(des);
				nodeDes.appendChild(nodeDivDes);
				//create node description
				nodeQuan = createNode("td");
				var quan = createTextNode(book.quan);
				nodeQuan.appendChild(quan);

				//create node image
				nodeTdImg = createNode("td");
				nodeImg = createNode("img");
				nodeImg.setAttribute("src",book.imgUrl);
				nodeImg.setAttribute("width",150);
				nodeImg.setAttribute("height",200);
				nodeTdImg.appendChild(nodeImg);

				//create node count
				nodeCount = createNode("td");
				var count = createTextNode(book.count);
				nodeCount.appendChild(count);

				//create node update
				nodeTdUpdate = createNode("td");
				nodeDivUpdate = createDiv();
				nodeDivUpdate.addClass("updateProduct");
				nodeUpdate = createNode("img");
				nodeUpdate.setAttribute("src", "Image/update.png");
				nodeUpdate.style.cursor = "pointer";
				nodeDivUpdate.appendChild(nodeUpdate);
				nodeTdUpdate.appendChild(nodeDivUpdate);
				//add onClick event listener for class updateProduct
				nodeTdUpdate.addEventListener("click", function(event) {
					var currentRow = $id(event.currentTarget.parentNode.id);
					var cels = currentRow.getElementsByTagName("td");
					var id = cels[0].textContent;
					var bookName = cels[1].textContent;
					var category = cels[2].textContent;
					var author = cels[3].textContent;
					var price = cels[4].textContent;
					var des = cels[5].textContent;
					var quan = cels[6].textContent;
					var imgUrl = cels[7].innerHTML;

					showPage("content-updateProduct");
					var model = createBookModel(id, bookName, category, author, price, des, quan, imgUrl);
						
					var data = createCategoryModel();
					createCateNode(data.categories, model.cate);
					createUpdateProductView(model.id, model.bname, model.author, model.price, model.des, model.quan, model.urlImg);

					
					//pagesManager.setPage(Pages.updateProduct, false, model);
				});

				//create node delete
				nodeTdDelete = createNode("td");
				nodeDivDelete = createDiv();
				nodeDelete = createNode("img");
				nodeDelete.setAttribute("src", "Image/delete_icon.png");
				nodeDelete.style.cursor = "pointer";
				nodeTdDelete.addEventListener("click", function() {
					var currentRow = $id(event.currentTarget.parentNode.id);
					var cels = currentRow.getElementsByTagName("td");
					var id = cels[0].textContent;
					var r = confirm("Bạn có muốn xóa sách có mã là " + id + " ?");
					var x;
					if (r == true) {
						deleteProduct(id);
					}
				});
				nodeDivDelete.appendChild(nodeDelete);
				nodeTdDelete.appendChild(nodeDivDelete);

				nodeTr.appendChild(nodeId);
				nodeTr.appendChild(nodeBName);
				nodeTr.appendChild(nodeCate);
				nodeTr.appendChild(nodeAu);
				nodeTr.appendChild(nodePrice);
				nodeTr.appendChild(nodeDes);
				nodeTr.appendChild(nodeQuan);
				nodeTr.appendChild(nodeTdImg);
				nodeTr.appendChild(nodeCount);
				nodeTr.appendChild(nodeTdUpdate);
				nodeTr.appendChild(nodeTdDelete);

				nodeTable.appendChild(nodeTr);

			}
			rootNode.appendChild(nodeTable);
			var divPaging = createDiv();
			divPaging.setAttribute("id", "pageNavPosition");
			divPaging.setAttribute("class", "paging");
			var script = createNode("script");
			script.setAttribute("type", "text/javascript");
			script.innerHTML = "var pager = new Pager('tableProduct', 5); pager.init(); pager.showPageNav('pager', 'pageNavPosition'); pager.showPage(1);";
			rootNode.appendChild(divPaging);
			rootNode.appendChild(script);
		}

}

function createBookModel(id, bname, cate, author, price, des, quan, urlImg){
	var BookModel = new Object;
	BookModel.id = id;
	BookModel.bname = bname;
	BookModel.cate = cate;
	BookModel.author = author;
	BookModel.price = price;
	BookModel.des = des;
	BookModel.quan = quan;
	BookModel.urlImg = urlImg;
	return BookModel;

}

function callProductListService() {
	ajax.get({
		url : "http://localhost:8080/book-service/staff/manageBook",
		success : function(data, xhr) {
			showPage("content-manageProduct");
			var model = createProductModel(data);
			if(model.books <= 0){
				alert("Không có kết quả!");
			}
			showManageBookPage(model.books);
			//pagesManager.setPage(Pages.manageProduct, false, model);
		},
		failure : function(xhr) {
			if (xhr.status == 401) {
				alert("Bạn không có quyền truy cập vào trang này!\n Xin mời đăng nhập lại");
				doRequestFail();
			} else {
				alert("Có lỗi xảy ra. Xin thử lại!");
			}
		},
		abort : function(xhr) {
			callback.abort(xhr);
		}
	});
}

function deleteProduct(id) {
	ajax.del({
		url : "http://localhost:8080/book-service/staff/manageBook",
		param : {
			"txtID" : id
		},
		success : function() {
			alert("Xóa sách có mã " + id + " thành công!");
			showPage("content-manageProduct");
			callProductListService();
		},
		failed : function(xhr) {
			if (xhr.status == 401) {
				alert("Bạn không có quyền truy cập vào trang này!\n Xin mời đăng nhập lại");
				doRequestFail();
			} else {
				alert("Có lỗi xảy ra. Xin thử lại!");
			}
		},
		abort : function(xhr) {
			callback.abort(xhr);
		}
	});
}

function searchBook() {
	var searchKey = document.getElementById("txtSearchBook").value;
	ajax.get({
		url : "http://localhost:8080/book-service/staff/manageBook/searchBook",
		param : {
			"txtSearchBook" : searchKey
		},
		success : function(data, xhr) {
			var model = createProductModel(data);
			if(model.books <= 0){
				alert("Không có kết quả!");
			}
			showManageBookPage(model.books);
		},
		failure : function(xhr) {
			if (xhr.status == 401) {
				alert("Bạn không có quyền truy cập vào trang này!\n Xin mời đăng nhập lại");
				doRequestFail();
			} else {
				alert("Có lỗi xảy ra. Xin thử lại!");
			}
		},
		abort : function(xhr) {
			callback.abort(xhr);
		}
	});
}

function searchHotBook() {
	ajax.get({
		url : "http://localhost:8080/book-service/staff/manageBook/HotBook",
		success : function(data, xhr) {
			var model = createProductModel(data);
			if(model.books <= 0){
				alert("Không có kết quả!");
			}
			showManageBookPage(model.books);
		},
		failure : function(xhr) {
			if (xhr.status == 401) {
				alert("Bạn không có quyền truy cập vào trang này!\n Xin mời đăng nhập lại");
				doRequestFail();
			} else {
				alert("Có lỗi xảy ra. Xin thử lại!");
			}
		},
		abort : function(xhr) {
			callback.abort(xhr);
		}
	});

}

function searchOutOfBook() {
	ajax.get({
		url : "http://localhost:8080/book-service/staff/manageBook/outOfBook",
		success : function(data, xhr) {
			var model = createProductModel(data);
			if(model.books <= 0){
				alert("Không có kết quả!");
			}
			showManageBookPage(model.books);
		},
		failure : function(xhr) {
			if (xhr.status == 401) {
				alert("Bạn không có quyền truy cập vào trang này!\n Xin mời đăng nhập lại");
				doRequestFail();
			} else {
				alert("Có lỗi xảy ra. Xin thử lại!");
			}
		},
		abort : function(xhr) {
			callback.abort(xhr);
		}
	});

}


var Book = function(books) {
	this.books = books;
}

function createProductModel (book) {
		var books = book.getElementsByTagName("book");
		var author, bookId, bookName, categoryId, count, des, price, quan, imgUrl;
		var arrBook = [];
		for (var i = 0; i < books.length; i++) {
			author = books[i].getElementsByTagName("author")[0].childNodes[0].nodeValue;
			bookId = books[i].getElementsByTagName("bookId")[0].childNodes[0].nodeValue;
			bookName = books[i].getElementsByTagName("bookName")[0].childNodes[0].nodeValue;
			categoryId = books[i].getElementsByTagName("categoryId")[0].childNodes[0].nodeValue;
			count = books[i].getElementsByTagName("count")[0].childNodes[0].nodeValue;
			des = books[i].getElementsByTagName("description")[0].childNodes[0].nodeValue;
			price = books[i].getElementsByTagName("price")[0].childNodes[0].nodeValue;
			quan = books[i].getElementsByTagName("quantity")[0].childNodes[0].nodeValue;
			imgUrl = books[i].getElementsByTagName("picture")[0].childNodes[0].nodeValue;
			arrBook[i] = {
				"bookId" : bookId,
				"author" : author,
				"bookName" : bookName,
				"categoryId" : categoryId,
				"count" : count,
				"des" : des,
				"price" : price,
				"quan" : quan,
				"imgUrl" : imgUrl
			};
		}

		return new Book(arrBook);

	}